package handling.login;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import provider.MapleData;
import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
import provider.MapleDataTool;
import server.ServerProperties;
import tools.Triple;

public class LoginInformationProvider {

    private static final LoginInformationProvider instance = new LoginInformationProvider();
    protected final List<String> ForbiddenName = new ArrayList();
    protected final List<String> Curse = new ArrayList();
    protected final Map<Triple<Integer, Integer, Integer>, List<Integer>> makeCharInfo = new HashMap();
    
    public static enum JobType {

        终极冒险家(-1, "终极冒险家", 0, 130000000, false, false, false, true, false),
        反抗者(0, "反抗者", 3000, 931000000, false, false, false, false, false),
        冒险家(1, "冒险家", 0, 103000002, false, false, false, false, false),
        骑士团(2, "骑士团", 1000, 913040000, false, false, false, false, true),
        战神(3, "战神", 2000, 914000000, false, false, false, true, false),
        龙神(4, "龙神", 2001, 900090000, false, false, false, true, false),
        双弩精灵(5, "双弩精灵", 2002, 910150000, false, false, false, false, false),
        恶魔(6, "恶魔猎手", 3001, 931050310, true, false, false, false, false),
        幻影(7, "幻影", 2003, 910000000, false, false, false, false, true),
        暗影双刀(8, "暗影双刀", 430, 910000000, false, false, false, false, false),
        米哈尔(9, "米哈尔", 5000, 910000000, false, false, false, true, false),
        夜光法师(10, "夜光法师", 2004, 910000000, false, false, false, false, true),
        狂龙战士(11, "狂龙战士", 6000, 910000000, false, false, false, false, false),
        爆莉萌天使(12, "爆莉萌天使", 6001, 910000000, false, false, false, false, false),
        火炮手(13, "火炮手", 501, 910000000, false, false, false, false, false),
        尖兵(14, "尖兵", 3002, 910000000, true, false, false, false, false),
        神之子(15, "神之子", 10112, 910000000, false, false, false, false, true),
        隐月(16, "隐月", 2005, 910000000, false, false, false, true, true),
        林之灵(17, "林之灵", 11000, 910000000, true, true, true, false, false),
        龙的传人(18, "龙的传人", 508, 910000000, false, false, false, true, false);
        
        public int type, id, map;
        public String job;
        //private boolean 自由市场 = true;
        public boolean 脸饰, 耳朵, 尾巴, 裤子, 披风;

        private JobType(int type, String job, int id, int map, boolean 脸饰, boolean 耳朵, boolean 尾巴, boolean 裤子, boolean 披风) {
            this.type = type;
            this.job = job;
            this.id = id;
            //this.map = map;
            this.脸饰 = 脸饰;
            this.耳朵 = 耳朵;
            this.尾巴 = 尾巴;
            this.裤子 = 裤子;
            this.披风 = 披风;

            this.map = (ServerProperties.getProperty("world.firstMap") != null ? Integer.parseInt(ServerProperties.getProperty("world.firstMap")) : 910000000);
        }

        public static JobType getByJob(String g) {
            for (JobType e : values()) {
                if ((e.job.length() > 0) && (g.startsWith(e.job))) {
                    return e;
                }
            }
            return 冒险家;
        }

        public static JobType getByType(int g) {
            for (JobType e : values()) {
                if (e.type == g) {
                    return e;
                }
            }
            return 冒险家;
        }

        public static JobType getById(int g) {
            for (JobType e : values()) {
                if (e.id == g) {
                    return e;
                }
            }
            return 冒险家;
        }
    }

    public static LoginInformationProvider getInstance() {
        return instance;
    }
    private List our;

    protected LoginInformationProvider() {
        String WZpath = System.getProperty("wzpath");
        MapleDataProvider prov = MapleDataProviderFactory.getDataProvider(new File(WZpath + "/Etc.wz"));
        MapleData nameData = prov.getData("ForbiddenName.img");
        for (MapleData data : nameData.getChildren()) {
            this.ForbiddenName.add(MapleDataTool.getString(data));
        }
        nameData = prov.getData("Curse.img");
        for (MapleData data : nameData.getChildren()) {
            this.Curse.add(MapleDataTool.getString(data).split(",")[0]);
            this.ForbiddenName.add(MapleDataTool.getString(data).split(",")[0]);
        }
        MapleData infoData = prov.getData("MakeCharInfo.img");

        for (MapleData dat : infoData) {
            int val = 0;
            int type;
            List our;
            try {
                type = JobType.getById(Integer.parseInt(dat.getName())).type;

                for (MapleData d : dat) {
                    if (d.getName().equals("male")) {
                        val = 0;
                    } else if (d.getName().equals("female")) {
                        val = 1;
                    }

                    for (MapleData da : d) {
                        Triple key = new Triple(Integer.valueOf(val), Integer.valueOf(Integer.parseInt(da.getName())), Integer.valueOf(type));
                        our = (List) this.makeCharInfo.get(key);
                        if (our == null) {
                            our = new ArrayList();
                            this.makeCharInfo.put(key, our);
                        }
                        for (MapleData dd : da) {
                            if (!dd.getName().equals("name")) {
                                our.add(Integer.valueOf(MapleDataTool.getInt(dd, -1)));
                            }
                        }
                    }
                }
            } catch (NumberFormatException e) {
            }
        }
        MapleData uA = infoData.getChildByPath("UltimateAdventurer");
        for (MapleData dat : uA) {
            Triple key = new Triple(Integer.valueOf(-1), Integer.valueOf(Integer.parseInt(dat.getName())), Integer.valueOf(JobType.终极冒险家.type));
            our = (List) this.makeCharInfo.get(key);
            if (our == null) {
                our = new ArrayList();
                this.makeCharInfo.put(key, our);
            }
            for (MapleData d : dat) {
                our.add(MapleDataTool.getInt(d, -1));
            }
        }
        List our;
    }

    public final boolean isForbiddenName(String in) {
        for (String name : this.ForbiddenName) {
            if (in.toLowerCase().contains(name.toLowerCase())) {
                return true;
            }
        }
        return false;
    }

    public final boolean isCurseMsg(String in) {
        for (String name : this.Curse) {
            if (in.toLowerCase().contains(name.toLowerCase())) {
                return true;
            }
        }
        return false;
    }

    public final boolean isEligibleItem(int gender, int val, int job, int item) {
        if (item < 0) {
            return false;
        }
        Triple key = new Triple(Integer.valueOf(gender), Integer.valueOf(val), Integer.valueOf(job));
        List our = (List) this.makeCharInfo.get(key);
        if (our == null) {
            return false;
        }
        return our.contains(item);
    }

    
}

